import os


def convert_encoding(file_path, from_encoding='GBK', to_encoding='UTF-8'):


with open(file_path, 'r', encoding=from_encoding, errors='ignore') as file:

content = file.read()

with open(file_path, 'w', encoding=to_encoding) as file:

file.write(content)


def batch_convert(directory, from_encoding='GBK', to_encoding='UTF-8'):


for root, _, files in os.walk(directory):

for file in files:

if file.endswith('.txt'):

file_path = os.path.join(root, file)

convert_encoding(file_path, from_encoding, to_encoding)

print(f'Converted {file_path} to {to_encoding}')


directory = 'path/to/your/txt/files'

batch_convert(directory)
